package pe.com.cineplanet.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 


import org.apache.commons.beanutils.PropertyUtils; 
import org.apache.struts.Globals; 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 


public class LocaleAction extends Action { 

    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws IllegalAccessException, InvocationTargetException, 
            NoSuchMethodException { 
    	
        String language = (String) PropertyUtils.getSimpleProperty(form, "lang");
        String evalSession = request.getParameter("evalSession");

        if (language != null && language.length() > 0) { 
        	
        	String[] locstr = language.split("_");
        	if(locstr.length==1) locstr = new String[]{ locstr[0], "" };
        	
            Locale locale = new java.util.Locale(locstr[0], locstr[1]); 
            HttpSession session = request.getSession(); 
            session.setAttribute(Globals.LOCALE_KEY, locale);
        } 
        if(evalSession.equals("true")) request.setAttribute("redirect", "login.htm?method=config");
        return mapping.findForward("success"); 
    } 
}  

